{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NoName\n",
      "sec\n",
      "Sleeping ... ...\n",
      "9527\n",
      "attention\n"
     ]
    }
   ],
   "source": [
    "# 继承的语法\n",
    "# 在python中，任何类都有一个共同的父类叫object\n",
    "\n",
    "class Person():\n",
    "    name = \"NoName\"\n",
    "    age = 18\n",
    "    __score = 0 # 考试成绩是秘密，只要自己知道\n",
    "    _petname = \"sec\" #小名，是保护的，子类可以用，但不能公用\n",
    "    def sleep(self):\n",
    "        print(\"Sleeping ... ...\")\n",
    "        \n",
    "#父类写在括号内\n",
    "class Teacher(Person):\n",
    "    teacher_id = \"9527\"\n",
    "    def make_test(self):\n",
    "        print(\"attention\")\n",
    "    \n",
    "t = Teacher()\n",
    "print(t.name)\n",
    "# 受保护不能外部访问，为啥这里可以\n",
    "print(t._petname)\n",
    "\n",
    "# 私有访问问题\n",
    "# 公开访问私有变量，报错\n",
    "#print(t.__score)\n",
    "\n",
    "t.sleep()\n",
    "print(t.teacher_id)\n",
    "t.make_test()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DaNa\n"
     ]
    }
   ],
   "source": [
    "# 子类和父类定义同一个名称变量，则优先使用子类本身\n",
    "class Person():\n",
    "    name = \"NoName\"\n",
    "    age = 18\n",
    "    __score = 0 # 考试成绩是秘密，只要自己知道\n",
    "    _petname = \"sec\" #小名，是保护的，子类可以用，但不能公用\n",
    "    def sleep(self):\n",
    "        print(\"Sleeping ... ...\")\n",
    " \n",
    "        \n",
    "#父类写在括号内\n",
    "class Teacher(Person):\n",
    "    teacher_id = \"9527\"\n",
    "    name = \"DaNa\"\n",
    "    def make_test(self):\n",
    "        print(\"attention\")\n",
    "        \n",
    "t = Teacher()\n",
    "print(t.name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "make some money\n",
      "attention\n"
     ]
    }
   ],
   "source": [
    "# 子类扩充父类功能的案例\n",
    "# 人由工作的函数， 老师也由工作的函数，但老师的工作需要讲课\n",
    "class Person():\n",
    "    name = \"NoName\"\n",
    "    age = 18\n",
    "    __score = 0 # 考试成绩是秘密，只要自己知道\n",
    "    _petname = \"sec\" #小名，是保护的，子类可以用，但不能公用\n",
    "    def sleep(self):\n",
    "        print(\"Sleeping ... ...\")\n",
    "    def work(self):\n",
    "        print(\"make some money\")\n",
    "        \n",
    "#父类写在括号内\n",
    "class Teacher(Person):\n",
    "    teacher_id = \"9527\"\n",
    "    name = \"DaNa\"\n",
    "    def make_test(self):\n",
    "        print(\"attention\")\n",
    "        \n",
    "    def work(self):\n",
    "        # 扩充父类的功能只需要调用父类相应的函数\n",
    "        #Person.work(self)\n",
    "        # 扩充父类的另一种方法\n",
    "        # super代表得到父类\n",
    "        super().work()\n",
    "        self.make_test()\n",
    "        \n",
    "t = Teacher()\n",
    "t.work()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I am init in dog\n"
     ]
    }
   ],
   "source": [
    "# 构造函数的概念\n",
    "\n",
    "class Dog():\n",
    "    # __init__就是构造函数\n",
    "    # 每次实例化的时候，第一个被自动的调用\n",
    "    # 因为主要工作是进行初始化，所以得名\n",
    "    def __init__(self):\n",
    "        print(\"I am init in dog\")\n",
    "\n",
    "# 实例话的时候，括号内的参数需要跟构造函数参数匹配\n",
    "kaka = Dog()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I am init in dog\n"
     ]
    }
   ],
   "source": [
    "# 继承中的构造函数 - 1\n",
    "\n",
    "class Animel():\n",
    "    pass\n",
    "\n",
    "class PaxingAni(Animel):\n",
    "    pass\n",
    "\n",
    "class Dog(PaxingAni):\n",
    "    # __init__就是构造函数\n",
    "    # 每次实例化的时候，第一个被自动的调用\n",
    "    # 因为主要工作是进行初始化，所以得名\n",
    "    def __init__(self):\n",
    "        print(\"I am init in dog\")\n",
    "# 实例话的时候，自动调用了Dog的构造函数\n",
    "kaka = Dog()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I am init in dog\n",
      " Paxing Dongwu\n"
     ]
    }
   ],
   "source": [
    "# 继承中的构造函数 - 2\n",
    "\n",
    "class Animel():\n",
    "    def __init__(self):\n",
    "        print(\"Animel\")\n",
    "\n",
    "class PaxingAni(Animel):\n",
    "    def __init__(self):\n",
    "        print(\" Paxing Dongwu\")\n",
    "\n",
    "class Dog(PaxingAni):\n",
    "    # __init__就是构造函数\n",
    "    # 每次实例化的时候，第一个被自动的调用\n",
    "    # 因为主要工作是进行初始化，所以得名\n",
    "    def __init__(self):\n",
    "        print(\"I am init in dog\")\n",
    "        \n",
    "# 实例话的时候，自动调用了Dog的构造函数\n",
    "# 因为找到了构造函数，则不在查找父类的构造函数\n",
    "kaka = Dog()\n",
    "\n",
    "# 猫没有写构造函数\n",
    "class Cat(PaxingAni):\n",
    "    pass\n",
    "\n",
    "# 此时应该自动调用构造函数，因为Cat没有构造函数，所以查找父类构造函数\n",
    "# 在PaxingAni中查找到了构造函数，则停止向上查找\n",
    "c = Cat()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I am init in dog\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "__init__() missing 1 required positional argument: 'name'",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-23-160de1fa11e4>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     24\u001b[0m \u001b[0;31m# 此时，由于Cat没有构造函数，则向上查找\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     25\u001b[0m \u001b[0;31m#  因为PaxingAni的构造函数需要两个参数，实例化的时候给了一个，报错\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 26\u001b[0;31m \u001b[0mc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mCat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: __init__() missing 1 required positional argument: 'name'"
     ],
     "output_type": "error"
    }
   ],
   "source": [
    "# 继承中的构造函数 - 3\n",
    "\n",
    "class Animel():\n",
    "    def __init__(self):\n",
    "        print(\"Animel\")\n",
    "\n",
    "class PaxingAni(Animel):\n",
    "    def __init__(self, name):\n",
    "        print(\" Paxing Dongwu {0}\".format(name))\n",
    "\n",
    "class Dog(PaxingAni):\n",
    "    # __init__就是构造函数\n",
    "    # 每次实例化的时候，第一个被自动的调用\n",
    "    # 因为主要工作是进行初始化，所以得名\n",
    "    def __init__(self):\n",
    "        print(\"I am init in dog\")\n",
    "        \n",
    "# 实例化Dog时，查找到Dog的构造函数，参数匹配，不报错      \n",
    "d = Dog()\n",
    "\n",
    "class Cat(PaxingAni):\n",
    "    pass\n",
    "\n",
    "# 此时，由于Cat没有构造函数，则向上查找\n",
    "#  因为PaxingAni的构造函数需要两个参数，实例化的时候给了一个，报错\n",
    "c = Cat()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Animel\n",
      "Animel\n"
     ]
    }
   ],
   "source": [
    "# 继承中的构造函数 - 4\n",
    "\n",
    "class Animel():\n",
    "    def __init__(self):\n",
    "        print(\"Animel\")\n",
    "\n",
    "class PaxingAni(Animel):\n",
    "    pass\n",
    "\n",
    "class Dog(PaxingAni):\n",
    "    pass\n",
    "        \n",
    "# 实例化Dog时，查找到Dog的构造函数，参数匹配，不报错      \n",
    "d = Dog()\n",
    "\n",
    "class Cat(PaxingAni):\n",
    "    pass\n",
    "\n",
    "# 此时，由于Cat没有构造函数，则向上查找\n",
    "#  因为PaxingAni的构造函数需要两个参数，实例化的时候给了一个，报错\n",
    "c = Cat()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'type'>\n",
      "Help on class super in module builtins:\n",
      "\n",
      "class super(object)\n",
      " |  super() -> same as super(__class__, <first argument>)\n",
      " |  super(type) -> unbound super object\n",
      " |  super(type, obj) -> bound super object; requires isinstance(obj, type)\n",
      " |  super(type, type2) -> bound super object; requires issubclass(type2, type)\n",
      " |  Typical use to call a cooperative superclass method:\n",
      " |  class C(B):\n",
      " |      def meth(self, arg):\n",
      " |          super().meth(arg)\n",
      " |  This works for class methods too:\n",
      " |  class C(B):\n",
      " |      @classmethod\n",
      " |      def cmeth(cls, arg):\n",
      " |          super().cmeth(arg)\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __get__(self, instance, owner, /)\n",
      " |      Return an attribute of instance, which is of type owner.\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __init__(self, /, *args, **kwargs)\n",
      " |      Initialize self.  See help(type(self)) for accurate signature.\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data descriptors defined here:\n",
      " |  \n",
      " |  __self__\n",
      " |      the instance invoking super(); may be None\n",
      " |  \n",
      " |  __self_class__\n",
      " |      the type of the instance invoking super(); may be None\n",
      " |  \n",
      " |  __thisclass__\n",
      " |      the class invoking super()\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(type(super))\n",
    "help(super)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
